package net.xiangcaowuyu.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import net.xiangcaowuyu.springcloud.alibaba.service.NacosPaymentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 博客：https://www.xiangcaowuyu.net
 * Description:
 *
 * @Author: 香草物语
 * DateTime: 2021-07-25 13:59
 */
@RestController
@Slf4j
@RequestMapping("/nacos/consumer")
public class NacosConsumerController {

    @Resource
    private NacosPaymentService nacosPaymentService;

    @GetMapping("/fallback/{id}")
    @SentinelResource(value = "fallback", blockHandler = "handlerBlockHandler", fallback = "handlerFallback", exceptionsToIgnore = {IllegalAccessException.class})
    public String fallback(@PathVariable("id") String id) throws IllegalAccessException {
        log.info("******************输出：" + id);
        if ("4".equals(id)) {
            throw new IllegalAccessException("IllegalAccessException,非法参数异常");
        } else if ("5".equals(id)) {
            throw new NullPointerException("空指针异常");
        }
        return nacosPaymentService.echo(id);
    }

    @GetMapping("/echo/{id}")
    public String echo(@PathVariable("id") String id) {
        log.info("******************输出：" + id);
        return nacosPaymentService.echo(id);
    }

    public String handlerBlockHandler(String id) throws IllegalAccessException {
        log.info("******************报错了，handlerBlockHandler：" + id);
        return id;
    }

}
